package cn.edu.sctu.sctu_lysd.view.act;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

import cn.edu.sctu.sctu_lysd.R;
import cn.edu.sctu.sctu_lysd.base.BaseActivity;
import cn.edu.sctu.sctu_lysd.biz.MyOrdersBiz;
import cn.edu.sctu.sctu_lysd.biz.RegisterBiz;
import cn.edu.sctu.sctu_lysd.databinding.ActivityMyOrdersBinding;
import cn.edu.sctu.sctu_lysd.presenter.MyOrdersPresenter;
import cn.edu.sctu.sctu_lysd.utils.L;
import cn.edu.sctu.sctu_lysd.utils.ScreenUtils;
import cn.edu.sctu.sctu_lysd.view.fra.MyOrdersLeftFra;
import cn.edu.sctu.sctu_lysd.view.fra.MyOrdersRightFra;

/**
 * Created by wangfuxin on 2017-04-08.
 */

public class MyOrdersActivity extends BaseActivity implements MyOrdersBiz.View {

    private MyOrdersBiz.Presenter mPresenter;
    private ActivityMyOrdersBinding mBinding;
    private ViewPager mViewPager;
    public ImageView mMoveImg;


    private int screenWidth;
    //当前选中的项
    int currentTab = -1;

    private List<Fragment> mFragments;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_my_orders;
    }

    @Override
    protected void initAfter(Object bind) {

        mBinding = (ActivityMyOrdersBinding) bind;

        screenWidth = ScreenUtils.getScreenWidth(this);

        mViewPager = mBinding.viewPager;

        mMoveImg = mBinding.moveImg;

        mFragments = new ArrayList<>();

        mFragments.add(new MyOrdersLeftFra());
        mFragments.add(new MyOrdersRightFra());


        mViewPager.setAdapter(new MyOrdersFraStatePagerAda(getSupportFragmentManager(), mFragments));

        mBinding.leftText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeView(0);
            }
        });


        mBinding.rightText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeView(1);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        initPresenter(new MyOrdersPresenter(this, this));
        mPresenter.start();
    }


    @Override
    public void initPresenter(MyOrdersBiz.Presenter presenter) {

        this.mPresenter = presenter;

        mPresenter.start();

    }

    public void myOrdersBack(View view){

        finish();

    }


    class MyOrdersFraStatePagerAda extends FragmentStatePagerAdapter {


        private List<Fragment> mFragmentList;


        public MyOrdersFraStatePagerAda(FragmentManager fm, List<Fragment> fragmentList) {
            super(fm);

            this.mFragmentList = fragmentList;
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {

            if (mFragmentList != null) {
                return mFragmentList.size();
            }
            return 0;
        }


        @Override
        public void startUpdate(ViewGroup container) {
            super.startUpdate(container);

        }

        @Override
        public void finishUpdate(ViewGroup container) {
            super.finishUpdate(container);

            //获取当前的视图是位于ViewGroup的第几个位置，用来更新对应的覆盖层所在的位置
            int currentItem = mViewPager.getCurrentItem();
            if (currentItem == currentTab) {
                return;
            }
            imageMove(mViewPager.getCurrentItem());
            currentTab = mViewPager.getCurrentItem();

        }
    }




    /**
     * 移动覆盖层
     * @param moveToTab 目标Tab，也就是要移动到的导航选项按钮的位置
     * 第一个导航按钮对应0，第二个对应1，以此类推
     */
    private void imageMove(int moveToTab) {
        int startPosition=0;
        int moveToPosition=0;

        startPosition=currentTab*(screenWidth/2);
        moveToPosition=moveToTab*(screenWidth/2);
        //平移动画
        TranslateAnimation translateAnimation=new TranslateAnimation(startPosition,moveToPosition, 0, 0);
        translateAnimation.setFillAfter(true);
        translateAnimation.setDuration(100);
        mMoveImg.startAnimation(translateAnimation);
    }

    //手动设置ViewPager要显示的视图
    private void changeView(int desTab)
    {
        mViewPager.setCurrentItem(desTab, true);
    }


}
